package com.example.big_event.service.impl2;

import com.example.big_event.mapper.ArticleMapper;
import com.example.big_event.pojo.Article;
import com.example.big_event.pojo.PageBean;
import com.example.big_event.service.ArticleService;
import com.example.big_event.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@Service
public class ArticleServiceImpl implements ArticleService {
   @Autowired
    private ArticleMapper articleMapper;
    //增加文章
    @Override
    public void add(Article article) {
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());

        Map<String,Object> map= ThreadLocalUtil.get();
        Integer userId=(Integer) map.get("id");//得到创建者的id

        article.setCreateUser(userId);
        articleMapper.add(article);
    }

    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        //创建pageBean对象
        PageBean<Article>pb=new PageBean<>();
        //开启分页查询  PageHelper
        PageHelper.startPage(pageNum,pageSize);


        //调用mapper
        Map<String,Object>map=ThreadLocalUtil.get();
        Integer userId=(Integer)map.get("id");
        List<Article> as=articleMapper.list(userId, categoryId,state);
        //page中提供了方法，可以获取PageHelper分页查询后  得到的总记录条数和当前页数据
        Page<Article>p=(Page<Article>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());

        return pb;
    }


    //获取文章详细信息，根据文章id，获取一篇的详细信息
    @Override
    public Article getDetail(Integer id) {
        return articleMapper.getDetail(id);
    }
}
